package com.javasm.sys.controller;

import com.javasm.commons.base.BaseController;
import com.javasm.commons.entity.AxiosResult;
import com.javasm.commons.entity.TableDatas;
import com.javasm.commons.enums.OperatorAction;
import com.javasm.commons.enums.OperatorType;
import com.javasm.sys.PosService;
import com.javasm.sys.annotations.MyLog;
import com.javasm.sys.SysPos;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;

import static com.javasm.commons.entity.AxiosResult.notExists;

@RestController
@RequestMapping("sysPos")
public class PosController extends BaseController {
    private static Logger l = LoggerFactory.getLogger(PosController.class);
    @Autowired
    private PosService ps;


    @DeleteMapping("delPos/{posId}")
    public AxiosResult delPosById(@PathVariable("posId") List<String> posId) {
        startPage();
        Integer flag = ps.delPosByIds(posId);
        return isok(flag == posId.size());
    }

    @DeleteMapping("{posId}")//根据id删除整条数据(永久删除)  //只有状态是停用的数据才能永久删除
    @MyLog(title = "岗位管理",operatorType = OperatorType.MANAGE,operationAction = OperatorAction.DELETE)
    public AxiosResult delById(@PathVariable String posId ) {
        startPage();
        Integer integer = ps.delByPosId(posId);
        return isok(integer==1);
    }
    @GetMapping("posName")//根据name模糊查询
    @MyLog(title = "岗位管理",operatorType = OperatorType.MANAGE,operationAction = OperatorAction.OTHER)
    public AxiosResult getByName(String posName) {
        Collection<SysPos> getByName = ps.getListByPosName(posName);
        if(getByName.size()<=0){
            return notExists();
        }
        return suc(getByName);
    }

    @GetMapping("posCode")//根据code模糊查询
    @MyLog(title = "岗位管理",operatorType = OperatorType.MANAGE,operationAction = OperatorAction.OTHER)
    public AxiosResult getByCode(String posCode) {
        Collection<SysPos> getByCode = ps.getListByPosCode( posCode);
        if(getByCode.size()<=0){
            return notExists();
        }

        return suc(getByCode);
    }
    @GetMapping("posState")//根据状态值查询
    @MyLog(title = "岗位管理",operatorType = OperatorType.MANAGE,operationAction = OperatorAction.OTHER)
    public AxiosResult getByState(String posState){
        Collection<SysPos> listByState = ps.getListByState(posState);
        if(listByState.size()<=0){
            return notExists();
        }
        return suc(listByState);
    }

    @GetMapping("buildTree")//查询全部数据(树形菜单)
    @MyLog(title = "岗位管理",operatorType = OperatorType.MANAGE,operationAction = OperatorAction.OTHER)
    public  AxiosResult getBuildTree(){
        //startPage();
        List<SysPos> posTree = ps.getList();
        return  suc(posTree);
    }

    @PutMapping("updatePos")//修改
    @MyLog(title = "岗位管理",operatorType = OperatorType.MANAGE,operationAction = OperatorAction.DELETE)
    public AxiosResult updatePos(@RequestBody  SysPos sysPos) {
        startPage();
        Integer flag = ps.updatePos(sysPos);
        return isok(flag==1);
    }
    @GetMapping("getAll")//获取所有数据
    @MyLog(title = "岗位管理",operatorType = OperatorType.MANAGE,operationAction = OperatorAction.OTHER)
    public TableDatas selectAll(SysPos sysPos){
        startPage();
        List<SysPos> all = ps.getAll();
        return new TableDatas(all);
    }
    @PostMapping("addPos")//添加
    @MyLog(title = "岗位管理",operatorType = OperatorType.MANAGE,operationAction = OperatorAction.INSERT)
    public AxiosResult addPos(@RequestBody SysPos sysPos) {
        startPage();
        Integer insert = ps.addPos(sysPos);
        return isok(insert == 1);
    }
    @GetMapping("updateState")//修改状态
    @MyLog(title = "岗位管理",operatorType = OperatorType.MANAGE,operationAction = OperatorAction.UPDATE)
    public AxiosResult updateState(String posState,String posId){
        startPage();
        Integer integer = ps.updateByIdState(posState,posId);
        return suc();
    }
    @GetMapping("selectAll")//搜索
    @MyLog(title = "岗位管理",operatorType = OperatorType.MANAGE,operationAction = OperatorAction.UPDATE)
    public TableDatas updateState( SysPos sysPos){
        startPage();
        List<SysPos> list = ps.selectAll(sysPos);
        return new TableDatas(list);
    }


}
